//
//  HCCallRecordsViewController.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCCallRecordsViewController.h"
#import "HCCallRecordsModel.h"

@interface HCCallRecordsViewController ()<UITableViewDataSource, UITableViewDelegate, HCCallRecordsModelDelegate>

@property (nonatomic, retain) UINavigationController* navigationController;
@property (nonatomic, retain) UITableView* tableView;

@end

@implementation HCCallRecordsViewController

@synthesize navigationController = m_navigationController;
@synthesize tableView = m_tableView;

- (id)init
{
    self = [super init];
    if (nil != self)
    {
        self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 80)] autorelease];
        [self.view addSubview:self.tableView];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [HCCallRecordsModel defaultModel].delegate =self;
        
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:self];
        [self.view addSubview:self.navigationController.view];
        self.title = @"通话记录";
        
        UIBarButtonItem* menuItem = [[[UIBarButtonItem alloc] initWithTitle:@"清除" style:UIBarButtonItemStylePlain target:self action:@selector(clearDataPress:)] autorelease];
        self.navigationItem.rightBarButtonItem = menuItem;
    }
    
    return self;
}

- (void)dealloc
{
    self.navigationController = nil;
    self.tableView = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clearDataPress:(UIBarButtonItem*)button
{
    [[HCCallRecordsModel defaultModel].callRecordsArray removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [HCCallRecordsModel defaultModel].callRecordsArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* key = @"TableViewCell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:key];
    
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:key] autorelease];
    }
    NSString* phoneNumber = [[HCCallRecordsModel defaultModel].callRecordsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = phoneNumber;
    
    return cell;
}

#pragma mark -- HCCallRecordsModelDelegate
- (void)callRecordsDataUpdated
{
    [self.tableView reloadData];
}

@end
